package br.edu.ufcg.lp2;

import java.util.ArrayList;
/**
 * This class represents a name,
 * keeps a name, its description and
 * a list of tags related to the name
 * 
 * @author João Pedro Leôncio <joopeeds@gmail.com>
 * @author Franco Stefano <stefano.tejo@gmail.com>
 * @author Fellype Cavalcante <fellype.cavalcante@gmail.com>
 */
public class Name implements Comparable<Name>{
	
	private String name;
	private String description;
	private ArrayList<Tag> tags;
	private ArrayList<Vote> votes;
	
	/**
	 *  
	 * @param name
	 * 		The name itself
	 * @param description
	 * 		The name's description
	 * @throws Exception
	 * 		If the name or the description are empty
	 */
	
	public Name(String name, String description) throws Exception {
		setName(name);
		setDescription(description);
		tags = new ArrayList<Tag>();
		votes = new ArrayList<Vote>();
	}

	/**
	 * Gets the name itself
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * Changes the name 
	 * @param name
	 * 		The name itself
	 * @throws Exception
	 * 		If the name is an empty string
	 */
	public void setName(String name) throws Exception {
		if (name.trim().length()==0) {
			throw new Exception ("The name cannot be empty");
		}
		this.name = name;
	}

	/**
	 * Gets the description of the name
	 * @return
	 * 		The description
	 */
	public String getDescription() {
		return description;
	}
	
	/**
	 * Changes the description of the name
	 * @param description
	 * 			The description of the name
	 * @throws Exception
	 * 		If the description is an empty string
	 */

	public void setDescription(String description) throws Exception {
		if (description.trim().length()==0) {
			throw new Exception ("The description cannot be empty");
		}
		this.description = description;
	}

	/**
	 * Adds a new tag to the name
	 * @param newTag
	 * 		The new tag
	 */
	public void addTag(Tag newTag) {
		tags.add(newTag);
	}
	
	/**
	 * Returns a string that describes the name
	 */
	@Override
	public String toString() {
		return("Name: "+ name + "\nDescription: " + description);
	}
	/**
	 * Returns the mean of the ratings given by the users to this name
	 * @return rating
	 * 		The name's rating 
	 */
	public double getRating() {
		if (votes.size() == 0)
			return -2;
		else {
			double mean = 0;
			for(Vote vote: votes)
				mean += vote.getRating();
			mean/=votes.size();
			return mean;
		}
	}
	

	/**
	 * Gets the list of user votes
	 * @return
	 */
	public ArrayList<Vote> getVotes() {
		return votes;
	}

	/**
	 * Sets the list of votes
	 * It is necessary to the clone() method
	 * @param votes
	 * 		The list of votes
	 */
	public void setVotes(ArrayList<Vote> votes) {
		this.votes = votes;
	}
	
	/**
	 * Sets the list of tags
	 * It is necessary to the clone() method
	 * @param tags
	 * 		The list of tags
	 */
	public void setTags(ArrayList<Tag> tags) {
		this.tags = tags;
	}

	/**
	 * Returns a clone of the name 
	 * with the same votes, tags, name and description
	 */
	public Name clone(){
		Name cloned = null;
		try {
			cloned = new Name(getName(), getDescription());
		} catch (Exception e) {
			e.printStackTrace();
		}
		cloned.setTags(getTags());
		cloned.setVotes(getVotes());
		return cloned;
	}
	/**
	 * Verify if an user has already voted for this name
	 * @param user
	 * @return bool
	 */
	public boolean userHasVoted(User user){
		for(Vote vote: votes) if (vote.getUser().equals(user)) return true;
		return false;
	}
	
	/**
	 * Adds a new vote to this name, 
	 * if the user hasn't voted for it
	 * @param vote
	 * 		A vote
	 * @throws Exception
	 * 		If the user has voted for it before...
	 */
	public void addVote(Vote vote) throws Exception{
		if(userHasVoted(vote.getUser())) throw new Exception("User has already voted for this name");
		votes.add(vote);
	}
	
	/**
	 * Compares if a name is equals to another
	 * A name is equals to another, if the names are equals
	 */
	@Override
	public boolean equals(Object obj){
		if(!(obj instanceof Name )) return false;
		return ((Name) obj ).getName().equals(this.getName());
	}

	/**
	 * Gets the list of tags in this name
	 * @return
	 */
	public ArrayList<Tag> getTags() {
		return tags;
	}
	
	/**
	 * Verify if this name has a given tag
	 * @param tagToFind
	 * @return
	 */
	public boolean hasTag(Tag tagToFind){
		for(Tag tag: tags) if (tagToFind.equals(tag)) return true;
		return false;
	}
	/**
	 * Compares this name to another
	 */
	@Override
	public int compareTo(Name o) {
		return name.compareTo(o.getName());
	}
}